package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Qualitytype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("QualitytypeController")
public class QualitytypeController extends CmiEmaController {

	@RequestMapping("/listQualitytypes")
	public ModelAndView listQualitytypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = qualitytypePaginationService.updatePagination(request, pagination);
		Set<Qualitytype> qualitytypes = qualitytypePaginationService.getPaginationQualitytypes(pagination);

		mav.addObject("qualitytypes", qualitytypes);
		mav.addObject("entityName", "qualitytype");
		mav.addObject("pagination", pagination);
		mav.setViewName("qualitytype/listQualitytypes");
		return mav;
	}

	public String listQualitytypes() {
		return "redirect:/listQualitytypes";
	}

	@RequestMapping("/viewQualitytype")
	public ModelAndView viewQualitytype(@RequestParam Integer qualitytypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("qualitytype", qualitytypeDAO.findQualitytypeByPrimaryKey(qualitytypeId));
		mav.setViewName("qualitytype/viewQualitytype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteQualitytypes")
	public ModelAndView confirmDeleteQualitytypes(
			@RequestParam(value = "deleteQualitytypesIds", required = true) Set<Integer> deleteQualitytypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Qualitytype> deleteQualitytypes = qualitytypeDAO.findQualitytypesByIds(deleteQualitytypesIds);
		Set<Qualitytype> referencedQualitytypes = qualitytypeService.loadReferenceQualitytypes(deleteQualitytypes);
		boolean hasReferenced;
		if (referencedQualitytypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteQualitytypes);
			hasReferenced = false;
		} else {
			deleteQualitytypes.removeAll(referencedQualitytypes);
			mav.addObject("referencedEntitys", referencedQualitytypes);
			mav.addObject("deleteEntitys", deleteQualitytypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Qualitytype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listQualitytypes");
		return mav;
	}

	@RequestMapping("/deleteQualitytype")
	public String deleteQualitytype(@RequestParam Integer qualitytypeId) {
		Qualitytype qualitytype = qualitytypeDAO.findQualitytypeByPrimaryKey(qualitytypeId);
		qualitytypeService.deleteQualitytype(qualitytype);
		return "forward:/listQualitytypes";
	}

	@RequestMapping("/confirmDeleteQualitytype")
	public ModelAndView confirmDeleteQualitytype(@RequestParam Integer qualitytypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("qualitytype", qualitytypeDAO.findQualitytypeByPrimaryKey(qualitytypeId));

		mav.setViewName("qualitytype/deleteQualitytype");

		return mav;
	}

	@RequestMapping("/deleteQualitytypes")
	public String deleteQualitytypes(
			@RequestParam(value = "deleteQualitytypesId", required = true) Set<Integer> deleteQualitytypesId) {
		qualitytypeService.deleteQualitytypes(deleteQualitytypesId);
		return "forward:/listQualitytypes";
	}

	@RequestMapping("/editQualitytype")
	public ModelAndView editQualitytype(@RequestParam Integer qualitytypeId) {

		ModelAndView mav = new ModelAndView();

		Qualitytype qualitytype = qualitytypeDAO.findQualitytypeByPrimaryKey(qualitytypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("qualitytype", qualitytype);

		// 把所有的引用qualitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(qualitytypeService.loadQualitytypeObjectReferencedSet(qualitytype));

		// 将编辑页面qualitytype添加到mav中
		mav.setViewName("qualitytype/editQualitytype");
		return mav;
	}

	@RequestMapping("/newQualitytype")
	public ModelAndView newQualitytype() {
		ModelAndView mav = new ModelAndView();
		Qualitytype qualitytype = new Qualitytype();

		mav.addObject("qualitytype", qualitytype);

		// 把所有的引用qualitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(qualitytypeService.loadQualitytypeObjectReferencedSet(qualitytype));

		mav.addObject("newFlag", true);
		mav.setViewName("qualitytype/editQualitytype");

		return mav;
	}

	@RequestMapping("/saveQualitytype")
	public ModelAndView saveQualitytype(@ModelAttribute Qualitytype qualitytype) {

		ModelAndView mav = new ModelAndView();

		Qualitytype existingQualitytype = CmiHibernateProxyUtil
				.deproxy(qualitytypeDAO.findQualitytypeByPhysicalPrimaryKey(qualitytype.getName()));

		// 新建记录
		if (qualitytype.getId() == null) {
			if (qualitytype.equals(existingQualitytype)) {

				mav.addAllObjects(qualitytypeService.loadQualitytypeClassReferencedSet(qualitytype));
				// mav = editContinue(mav, qualitytype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("qualitytype/editQualitytype");
			} else {
				qualitytypeService.saveQualitytype(qualitytype);
				mav.setViewName("forward:/listQualitytypes");
			}
		}
		// 编辑记录
		else {
			Qualitytype originQualitytype = CmiHibernateProxyUtil
					.deproxy(qualitytypeDAO.findQualitytypeByPrimaryKey(qualitytype.getId()));
			if (!qualitytype.equals(originQualitytype) && qualitytype.equals(existingQualitytype)) {
				mav.addAllObjects(qualitytypeService.loadQualitytypeClassReferencedSet(qualitytype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("qualitytype/editQualitytype");
			} else {
				qualitytypeService.saveQualitytype(qualitytype);
				mav.setViewName("forward:/listQualitytypes");
			}
		}
		return mav;
	}

	@RequestMapping("/qualitytypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
